package util;

import model.Fengmian;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/6/13 0013.
 */
public class FileUtil {
    public static final String FILE_PATH = "C:\\Users\\Administrator\\Desktop\\A3\\01经济类";

    public static List<Fengmian> fengmianList = new ArrayList<Fengmian>();
    public static void main(String[] args) {
//        System.out.println("start..................");
//        File f = new File(FILE_PATH);
//        fun(f);
//        for (Fengmian fengmian : fengmianList) {
//            System.out.println(fengmian);
//        }
//        System.out.println("end..................");

        String s = "C:\\Users\\Administrator\\Desktop\\A3\\川改进(周一晚).docx";
        String t = "C:\\Users\\Administrator\\Desktop\\A3\\t.docx";
        Date start = new Date();
//        fileChannelCopy(new File(s),new File(t));
        copy(new File(s),new File(t));
        Date end = new Date();
        System.out.println("用时（毫秒）："+(end.getTime() - start.getTime()));
    }

    //要不断列出，因为给出的File类对象可能是一个目录
    public static void fun(File f){
        //判断给定的路径是否是目录，如果是目录在列出
        if(f.isDirectory()){
            File[] file = f.listFiles();
            //再依次循环进行判断
            try{
                for(int i = 0;i < file.length;i++){
                    //继续把内容传入到fun方法之中进行验证
                    fun(file[i]);
                }
            }catch(Exception e){}
        }
        else{
            String path = f.getParentFile().getAbsolutePath();
            String fileName = f.getName();
            Fengmian fengmian = new Fengmian();
            fengmian.setPath(path);
            fengmian.setFileName(fileName);
            fengmianList.add(fengmian);
        }
    }

    /**
     * 使用文件通道的方式复制文件
     * @param s  源文件
     * @param t 复制到的新文件
     */
    public static void fileChannelCopy(File s,File t){
        FileInputStream fi =  null;
        FileOutputStream fo = null;
        FileChannel in = null;
        FileChannel out = null;
        try {
            fi = new FileInputStream(s);
            fo = new FileOutputStream(t);
            in = fi.getChannel();//得到对应的文件通道
            out = fo.getChannel();//得到对应的文件通道
            in.transferTo(0,in.size(),out);//连接两个通道，并且从in通道读取，然后写入out通道
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fi.close();
                in.close();
                fo.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 普通的缓冲输入输出流的复制
     * @param s
     * @param t
     */
    public static void copy(File s,File t){
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new BufferedInputStream(new FileInputStream(s));
            os = new BufferedOutputStream(new FileOutputStream(t));
            byte[] buf = new byte[2048];
            int i;
            while((i = is.read(buf))!=-1){
                os.write(buf,0,i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
